import java.io.*;

public class FileUtil {

    public static String readString(String path) {
        BufferedInputStream bis = null;
        try {
            File file = new File(path);
            if (!file.exists()) {
                return "";
            }
            FileInputStream fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            StringBuilder content = new StringBuilder();
            //自定义缓冲区
            byte[] buffer = new byte[10240];
            int flag = 0;
            while ((flag = bis.read(buffer)) != -1) {
                content.append(new String(buffer, 0, flag));
            }
            return content.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭的时候只需要关闭最外层的流就行了
            try {
                if (bis != null) {
                    bis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    public static void writeFile(String filePath, String content) {
        try {
            FileOutputStream fos=new FileOutputStream(filePath);
            BufferedOutputStream bos=new BufferedOutputStream(fos);
            bos.write(content.getBytes(),0,content.getBytes().length);
            bos.flush();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
